/*
  TODO:
  - group by steps of 5 instead of steps of 10
  - fix problem when only a value of 4 and 5 is present.
  - positioning of title
  - manual colorwheel attached to value, not to global (needed for magic website)
*/

/*
 * |       * * *
   |       *   *
   | * * * *   *
   | *   * *   *
   | *   * *   *
    ------------------
   |------------------|
        delta_data
   |------------|
      bar_size
   |------|
     sub_bar_size
 */

function KiwiChart(ID, chartType)
{
  //general information
  this.ID = ID;
  this.element = document.getElementById(ID);
  this.canvas = this.element.getContext('2d');
  this.chartType = (typeof chartType === 'undefined') ? "BarChart" : chartType;

  //dimensions
  this.height = this.element.height;
  this.width = this.element.width;

  //title
  this.title = "";
  this.titleMargin = 5;
  this.titleFontSize = 35;
  
  //Offsets
  this.leftOffset = this.calculateLeftOffset();
  this.rightOffset = this.calculateRightOffset();
  this.topOffset = this.calculateTopOffset();
  this.bottomOffset = this.calculateBottomOffset();
  
  //data
  this.plots = Array();
  this.numPlots = 0;
  
  //events
  this.element.addEventListener('mousedown', this, false);
  this.element.addEventListener('mouseup', this, false);
  this.element.addEventListener('mouseout',this, false);
  this.element.addEventListener('mousewheel', this, false);
  this.element.addEventListener('DOMMouseScroll', this, false);
  
  //first draw
  this.redraw();
}

/* Names */
KiwiChart.prototype.setTitle = function(newTitle, size)
{
  if(typeof size !== 'undefined')
  {
    this.titleFontSize = parseInt(size);
  }
  this.title = newTitle;
  this.topOffset = this.calculateTopOffset();
  
  this.redraw();
};

/* Common Functions */

KiwiChart.prototype.calculateLeftOffset = function()
{
  return 5;
};

KiwiChart.prototype.calculateTopOffset = function()
{
  var topOffset = 5;

  if(this.title !== "")
  {
    topOffset += this.titleFontSize + 2*this.titleMargin;
  }
  
  return topOffset;
};

KiwiChart.prototype.calculateRightOffset = function()
{
  return 5;
};

KiwiChart.prototype.calculateBottomOffset = function()
{
  return 5;
};

KiwiChart.prototype.roundNumberDown = function(number, scale)
{
  if(typeof number === 'undefined')
  {
    return 0;
  }
 
  if(typeof scale === 'undefined')
  {
    scale = 0;
  }
 
  if(number > 0)
  {
    if(number % Math.pow(10,scale))
    {
      return number - number%(Math.pow(10,scale));
    }
    return number;
  }
  else if(number < 0)
  {
    return number - ((number%(Math.pow(10,scale)))+(Math.pow(10,scale)))%(Math.pow(10,scale));
  }
  
  return 0;
};

KiwiChart.prototype.roundNumberUp = function(number, scale)
{
  if(typeof number === 'undefined')
  {
    return 0;
  }
 
  if(typeof scale === 'undefined')
  {
    scale = 0;
  }
 
  if(number > 0)
  {
    if(number % Math.pow(10,scale)/2)
    {
      return number + (Math.pow(10,scale)/2 - number%(Math.pow(10,scale)/2));
    }
    return number;
  }
  if(number < 0)
  {
    return number - ((number%(Math.pow(10,scale)))+(Math.pow(10,scale)))%(Math.pow(10,scale));
  }
  
  return 0;
};

KiwiChart.prototype.bubbleShort = function(array, names, direction)
{
  /*
    with credits to: 
    Stoimen
    http://www.stoimen.com/blog/2010/07/09/friday-algorithms-javascript-bubble-sort/
  */
  var swapped;
  if(direction === "DESC")
  {
    do {
      swapped = false;
      for (var i=0; i < array.length-1; i++) {
          if (array[i] < array[i+1]) {
              var temp = array[i];
              array[i] = array[i+1];
              array[i+1] = temp;
              
              var temp = names[i];
              names[i] = names[i+1];
              names[i+1] = temp;
              
              swapped = true;
          }
      }
    } while (swapped);
  }
  else
  {
    do {
        swapped = false;
        for (var i=0; i < array.length-1; i++) {
            if (array[i] > array[i+1]) {
                var temp = array[i];
                array[i] = array[i+1];
                array[i+1] = temp;
                
                var temp = names[i];
                names[i] = names[i+1];
                names[i+1] = temp;
                
                swapped = true;
            }
        }
    } while (swapped);
  }
};

/* forces recalculations of (co)domain*/
KiwiChart.prototype.ForceCalculateMinMaxValue = function()
{
  this.MinDataValue = 0;
  this.MaxDataValue = 'NaN';
  this.MinBarLegendaValue = 'NaN';
  this.MaxBarLegendaValue = 'NaN';

  for(plotID in this.plots)
  {
    this.CalculateMinMaxValue(plotID);
  }
};

/* Draw */
KiwiChart.prototype.redraw = function()
{
  //clear canvas
  this.canvas.clearRect( 0, 0, this.width, this.height);

  this.drawTitles();
};

/* Draws the Title (and in the future subtitle) of the barchart*/
KiwiChart.prototype.drawTitles = function()
{
  this.canvas.fillStyle = 'black';
  this.canvas.font = this.titleFontSize+'px Consolas';
  
  var textDrawLength = this.title.toString().length*(this.titleFontSize*(0.49 + 0.05));
  
  var factor = 1.4355 * Math.pow(this.titleFontSize, -0.113);
  
  this.canvas.fillText(this.title, this.width/2 - textDrawLength/2, this.titleFontSize * factor);
};

KiwiChart.prototype.drawLine = function(x1, x2, y1, y2, color, strokewidth)
{
  this.canvas.lineWidth = (typeof strokewidth !== 'undefined') ? parseInt(strokewidth) : 1;
  this.canvas.strokeStyle = color;
  this.canvas.beginPath();
  this.canvas.moveTo(x1, y1);
  this.canvas.lineTo(x2, y2);
  this.canvas.stroke();
};

KiwiChart.prototype.drawRectangle = function(x, y, width, height, color, border, bordercolor, borderwidth)
{
  var temp_fillStyle = this.canvas.fillStyle;
  this.canvas.fillStyle = (typeof color === 'undefined') ? "black" : color;
  
  this.canvas.beginPath();
  this.canvas.moveTo(x, y);
  this.canvas.rect(x, this.height-y-height, width, height);
  this.canvas.fill();
  
  if(border)
  {
    var temp_strokeStyle = this.canvas.strokeStyle;
    var temp_lineWidth = this.canvas.lineWidth;
    this.canvas.strokeStyle = bordercolor;
    this.canvas.lineWidth = borderwidth;
    
    this.canvas.stroke();
    
    this.canvas.strokeStyle = temp_strokeStyle;
    this.canvas.lineWidth = temp_lineWidth;
  }
  
  this.canvas.fillStyle = temp_fillStyle;
};

/* Data */
KiwiChart.prototype.addPlot = function(plotID)
{
  if(!this.plots[plotID])
  {
    this.plots[plotID] = Array();
    this.numPlots++;
  }
};

KiwiChart.prototype.setPlotColor = function(plotID, color)
{
  this.addPlot(plotID);
  this.plots[plotID].color = color;
};

KiwiChart.prototype.setPlotData = function(plotID, data)
{
  if(typeof data === 'undefined')
  {
    return;
  }

  this.addPlot(plotID);
  this.plots[plotID].data = Array();
  
  for(subdata in data)
  {
    this.plots[plotID].data[subdata] = data[subdata];
  }
  this.redraw();
};

/* Event handler */
KiwiChart.prototype.handleEvent = function(event, arg)
{
  
};

/**
 *
 *  Kiwi Bar Chart
 *
 **/
function KiwiBarChart(ID)
{
  //XLabel
  this.XLabel = "";
  this.XTextMargin = 5;
  this.XValuesFontSize = 10;
  this.XLabelFontSize = 10;
  
  //YLabel
  this.YLabel = "";
  this.YTextMargin = 5;
  this.YValuesFontSize = 10;
  this.YLabelFontSize = 10;
  
  KiwiChart.call(this, ID);
    
  //leganda
  this.ValueFontSize = 15;
  this.MinDataValue = 0;
  this.MaxDataValue = 'NaN';
  this.MinBarLegendaValue = 'NaN';
  this.MaxBarLegendaValue = 'NaN';
  
  this.verticalScale = 0;
  this.horizontalScale = 0;
  
  this.redraw();
}

KiwiBarChart.prototype = Object.create(KiwiChart.prototype);

KiwiBarChart.prototype.calculateLeftOffset = function()
{
  var leftOffset = 0;

  var longestValue = 1;
  if(this.MinDataValue || this.MaxDataValue)
  {
    var longestValue = (this.MaxDataValue.toString().length > this.MinDataValue.toString().length) ? this.MaxDataValue: this.MinDataValue;
  }
  leftOffset += longestValue.toString().length*(this.YValuesFontSize*(0.49 + 0.05)) + this.YTextMargin*2;
  if(this.YLabel !== "")
  {
    leftOffset += this.YLabelFontSize + this.XTextMargin;
  }
  
  return leftOffset;
};

KiwiBarChart.prototype.calculateBottomOffset = function()
{
  var bottomOffset = 0;
    
  bottomOffset += this.XValuesFontSize + 2*this.XTextMargin;
    
  if(this.XLabel !== "")
  {
    bottomOffset += this.XLabelFontSize + this.XTextMargin;
  }
    
  return bottomOffset;
};

KiwiBarChart.prototype.CalculateMinMaxValue = function(plotID)
{
  for(data in this.plots[plotID].data)
  {
    if(parseInt(data) > this.MaxBarLegendaValue || isNaN(this.MaxBarLegendaValue))
    {
      this.MaxBarLegendaValue = parseInt(data);
    }
    
    if(parseInt(data) < this.MinBarLegendaValue || isNaN(this.MinBarLegendaValue))
    {
      this.MinBarLegendaValue = parseInt(data);
    }
  }
  
  var absoluteMaxBarLegendaValue = Math.max(this.MinBarLegendaValue, this.MaxBarLegendaValue);
  this.horizontalScale = Math.floor(Math.log(absoluteMaxBarLegendaValue)/Math.LN10);
  if(this.horizontalScale < 0)
  {
    this.horizontalScale = 0;
  }
  this.MinBarLegendaValue = this.roundNumberDown(this.MinBarLegendaValue, this.horizontalScale);
  this.MaxBarLegendaValue = this.roundNumberUp(this.MaxBarLegendaValue, this.horizontalScale);
  
  if(this.MaxBarLegendaValue === this.MinBarLegendaValue)
  {
    this.MaxBarLegendaValue++;
    this.MinBarLegendaValue--;
  }
  
  var incrementor = (this.MaxBarLegendaValue <= Math.pow(10, this.horizontalScale + 1)/2) ? Math.pow(10, this.horizontalScale)/2 : Math.pow(10, this.horizontalScale);

  for(var a = this.MinBarLegendaValue ; a <= this.MaxBarLegendaValue ; a += incrementor)
  {
    var data = 0;
    for(var b = a ; b < a + incrementor ; b++)
    {
      if(this.plots[plotID].data && this.plots[plotID].data[b])
      {
        data += this.plots[plotID].data[b];
      }
    }
    
    if(parseFloat(data) > this.MaxDataValue || isNaN(this.MaxDataValue))
    {
      this.MaxDataValue = parseFloat(data);
    }
    if(parseFloat(data) < this.MinDataValue || isNaN(this.MinDataValue))
    {
      this.MinDataValue = parseFloat(data);
    }
  }
  var absoluteMaxData = Math.max(this.MinDataValue, this.MaxDataValue);
  this.verticalScale = Math.floor(Math.log(absoluteMaxData)/Math.LN10);
  this.MinDataValue = this.roundNumberDown(this.MinDataValue, this.verticalScale);
  this.MaxDataValue = this.roundNumberUp(this.MaxDataValue, this.verticalScale);
  
  if(this.MaxDataValue === this.MinDataValue)
  {
    this.MaxDataValue++;
    this.MinDataValue--;
  }
    
  this.leftOffset = this.calculateLeftOffset();
};

KiwiBarChart.prototype.setPlotData = function(plotID, data)
{
  if(typeof data === 'undefined' || data.length == 0)
  {
    return;
  }

  this.addPlot(plotID);
  this.plots[plotID].data = Array();
  
  for(subdata in data)
  {
    this.plots[plotID].data[subdata] = data[subdata];
  }
  
  this.CalculateMinMaxValue(plotID);
  this.redraw();
};

KiwiBarChart.prototype.redraw = function()
{
  KiwiChart.prototype.redraw.call(this);

  this.drawLegenda();
  this.drawBars();
};

/* Draw values on the axis and axis titles */
KiwiBarChart.prototype.drawLegenda = function()
{
  var incrementor = (this.MaxBarLegendaValue <= Math.pow(10, this.horizontalScale + 1)/2) ? Math.ceil(Math.pow(10, this.horizontalScale)/2) : Math.pow(10, this.horizontalScale);
  
  var barWidth = (this.width - this.leftOffset - this.rightOffset)/Math.floor((this.roundNumberUp(this.MaxBarLegendaValue+1) - this.MinBarLegendaValue)/incrementor);
  this.canvas.fillStyle = 'black';
  
  // X-axis
  this.canvas.font = this.XValuesFontSize + 'px courier new';

  if(typeof this.XLabel !== 'undefined' && this.XLabel !== "")
  {
    this.canvas.save();
  
    var valueX_Ypos = this.height - (this.bottomOffset - this.XTextMargin*2 - this.XValuesFontSize - this.XLabelFontSize*0.8);
  
    var drawWidth = (this.width - this.leftOffset - this.rightOffset);
    var textDrawLength = this.XLabel.toString().length*(this.XLabelFontSize*(0.49 + 0.05));

    this.canvas.font = this.XLabelFontSize + 'px Consolas';
    this.canvas.fillText(this.XLabel, this.leftOffset + drawWidth/2 - textDrawLength/2, valueX_Ypos);
    
    this.canvas.restore();
  }

  var valueX_Ypos = this.height - (this.bottomOffset - this.XTextMargin - this.XValuesFontSize);
  
  for(var a = this.MinBarLegendaValue ; a <= this.MaxBarLegendaValue + 1 ; a += incrementor)
  {
    this.canvas.fillText(a, (a - this.MinBarLegendaValue)/incrementor * barWidth - a.toString().length*3 + this.leftOffset, valueX_Ypos);
    
    this.canvas.fillText('-', (a - this.MinBarLegendaValue)/incrementor * barWidth + barWidth/2 - 3 + this.leftOffset, valueX_Ypos);
    
    this.drawLine((a - this.MinBarLegendaValue)/incrementor * barWidth - 1 + this.leftOffset, (a - this.MinBarLegendaValue)/incrementor * barWidth - 1 + this.leftOffset, this.height - this.bottomOffset, this.topOffset, 'rgba(0, 0, 0, 0.3)');
  }
  
  // Y-axis
  if(typeof this.YLabel !== 'undefined' && this.YLabel !== "")
  {
    var temp_font = this.canvas.font;
  
    this.canvas.font = this.YLabelFontSize + 'px Consolas';
  
    this.canvas.save();
 
    this.canvas.translate(this.YLabelFontSize*0.795 + this.XTextMargin, this.topOffset + (this.height - this.topOffset - this.bottomOffset)/2);
    this.canvas.rotate(-0.5 * Math.PI);
    
    this.canvas.beginPath();
    this.canvas.fillText(this.YLabel, -this.YLabel.toString().length*(this.YLabelFontSize*(0.49 + 0.05))/2, 0);
    this.canvas.fill();
    
    this.canvas.restore();
    
    this.canvas.font = temp_font;
  }
  
  var incrementor = (this.MaxDataValue <= Math.pow(10, this.verticalScale + 1)/2) ? Math.ceil(Math.pow(10, this.verticalScale)/2) : Math.pow(10, this.verticalScale);
  
  var heightPerCount = (this.height - this.topOffset - this.bottomOffset)/(this.MaxDataValue - this.MinDataValue);
  for(var a = this.MinDataValue ; a <= (this.MaxDataValue) ; a += incrementor)
  {
    var printedNumber = a.toPrecision(1);
  
    var textX = this.leftOffset - printedNumber.toString().length*6 - this.YTextMargin;
    var textY = (this.height + this.MinDataValue*heightPerCount) - a*heightPerCount - this.bottomOffset;
    this.canvas.fillText(printedNumber, textX, textY);
    this.drawLine(this.leftOffset, this.width - this.rightOffset, (this.height + this.MinDataValue*heightPerCount) - a*heightPerCount - this.bottomOffset, (this.height + this.MinDataValue*heightPerCount) - a*heightPerCount - this.bottomOffset, 'rgba(0, 0, 0, 0.3)');
  }
};

KiwiBarChart.prototype.drawBars = function()
{
  var BAROFFSET = 6;
  
  var incrementor = (this.MaxBarLegendaValue <= Math.pow(10, this.horizontalScale + 1)/2) ? Math.ceil(Math.pow(10, this.horizontalScale)/2) : Math.pow(10, this.horizontalScale);
  
  //width
  var drawWidth = (this.width - this.leftOffset - this.rightOffset);
  var barWidth = (this.width - this.leftOffset - this.rightOffset)/Math.floor((this.roundNumberUp(this.MaxBarLegendaValue+1) - this.MinBarLegendaValue)/incrementor);
  var subBarWidth = barWidth/this.numPlots;
  
  //height
  var drawHeight = (this.height - this.topOffset - this.bottomOffset);
  var heightPerCount = drawHeight/(this.MaxDataValue - this.MinDataValue);
  
  var YbarStart = (this.MinDataValue < 0) ? this.bottomOffset - this.MinDataValue*heightPerCount : this.bottomOffset;

  this.canvas.font = this.ValueFontSize + 'px courier new';
  
  var barCount = 0;
  for(plotID in this.plots)
  {
    for(var a = this.MinBarLegendaValue ; a < this.roundNumberUp(this.MaxBarLegendaValue+1) ; a += incrementor)
    {
      var data = 0;
    
      for(var b = a ; b < a + incrementor ; b++)
      {
        if(this.plots[plotID].data && this.plots[plotID].data[b])
        {
          data += this.plots[plotID].data[b];
        }
      }
      
      //start position
      var x = (a/incrementor - this.MinBarLegendaValue/incrementor) * barWidth + barCount*subBarWidth + this.leftOffset;
      
      this.drawRectangle(x + BAROFFSET/2, YbarStart, subBarWidth - BAROFFSET, data*heightPerCount, this.plots[plotID].color);
      
      this.canvas.save();
      this.canvas.fillStyle = 'black';
      this.canvas.shadowBlur=0;
      this.canvas.shadowOffsetX = 1;
      this.canvas.shadowOffsetY = 1;
      this.canvas.shadowColor = 'white';
      var printedData = data.toPrecision(1);
      this.canvas.fillText(printedData, x + subBarWidth/2 - printedData.toString().length*6, this.height - data*heightPerCount - YbarStart + 5);
      this.canvas.restore();
    }
    barCount++;
  }
};

KiwiBarChart.prototype.setXLabel = function(newLabel, size)
{
  if(typeof size !== 'undefined')
  {
    this.XLabelFontSize = parseInt(size);
  }

  this.XLabel = newLabel;
  this.bottomOffset = this.calculateBottomOffset();
};

KiwiBarChart.prototype.setYLabel = function(newLabel, size)
{
  if(typeof size !== 'undefined')
  {
    this.YLabelFontSize = parseInt(size);
  }

  this.YLabel = newLabel;
  this.calculateLeftOffset();
};

/**
 *
 *  Kiwi Pie Chart
 *
 **/
function KiwiPieChart(ID)
{
  KiwiChart.call(this, ID);
  
  this.PieColorType = "default";
  this.PieColorwheel = Array();
  
  this.redraw();
}

KiwiPieChart.prototype = Object.create(KiwiChart.prototype);

KiwiPieChart.prototype.redraw = function()
{
  KiwiChart.prototype.redraw.call(this);
  
  this.drawPie();
};

KiwiPieChart.prototype.generateColor = function(number, colortype)
{
/*
 * TODO: turn this into a function.
 */
  if(colortype === "manual")
  {
    if(this.PieColorwheel && this.PieColorwheel[number])
    {
      return this.PieColorwheel[number];
    }
    return "black";
  }
  if(colortype === "grayscale")
  {
    var value = 128 - number*24*Math.pow(-1, number)/2; //alternating over 128
    return 'rgba('+value+','+value+','+value+', 1)';
  }
  else
  {
    switch(number)
    {
    case(0):
      return 'rgba(256, 0, 0, 1)';
    case(1):
      return 'rgba(0, 256, 0, 1)';
    case(2):
      return 'rgba(0, 0, 256, 1)';
    case(3):
      return 'rgba(0, 0, 0, 1)';
    case(4):
      return 'rgba(256, 256, 0, 1)';
    case(5):
      return 'rgba(256, 0, 256, 1)';
    case(6):
      return 'rgba(0, 256, 256, 1)';
    case(7):
      return 'rgba(128, 128, 128, 1)';
    default:
      return 'black';
    }
  }
};

KiwiPieChart.prototype.drawLegenda = function(names, count, plotnumber, values, numValues)
{
  var lineHeight;
  if((lineHeight = (this.height - this.topOffset - this.bottomOffset)/(numValues * 2)) > 18)
  {
    lineHeight = 18;
  }
  var squaresize;
  if((squaresize = lineHeight - 3) > 15)
  {
    squaresize = 15;
  }
  
  if(typeof values !== 'undefined')
  {
    var total = 0;

    for(var a = 0 ; a < numValues ; a++)
    {
      total += values[a];
    }
  }
  
  if(typeof plotnumber === 'undefined')
  {
    plotnumber = 0;
  }

  var longest_tag = 0;
  for(var b = 0 ; b < count ; b++)
  {
    if(names[b].toString().length > longest_tag)
    {
      longest_tag = names[b].toString().length;
    }
  }
  
  var leftoffset = (plotnumber+1) * (this.width/this.numPlots) - longest_tag*9 - this.rightOffset;
  
  var maxLines = numValues;
  if(typeof values !== 'undefined')
  {
    maxLines *= 2;
  }
  var position = this.topOffset + (this.height - this.topOffset - this.bottomOffset - maxLines*lineHeight)/2 + lineHeight;
  
  this.canvas.font = squaresize+'px courier new';
  
  for(var b = 0 ; b < count ; b++)
  {
    this.drawRectangle(leftoffset - 20, this.height - position, squaresize, squaresize, this.generateColor(b, this.PieColorType), true, "black", 0.9);
    this.canvas.fillStyle = 'black';
    this.canvas.fillText(names[b], leftoffset, position - 4);
    position += lineHeight;
    if(typeof values !== 'undefined')
    {
      this.canvas.fillText(Math.round(values[b]/total * 100)+'%', leftoffset, position - 4);
      position += lineHeight;
    }
  }
  
  return 20 + longest_tag*9 + this.rightOffset;
};

KiwiPieChart.prototype.drawPie = function()
{
  var plot = 0;
  this.canvas.font = '15px courier new';
  for(plots in this.plots)
  {
    var values = Array();
    var names = Array();
    var total = 0;
    var a = 0;
    for(data in this.plots[plots].data)
    {
      total += Math.abs(this.plots[plots].data[data]);
      values[a] = Math.abs(this.plots[plots].data[data]);
      names[a] = data;
      a++;
    }
    
    this.bubbleShort(values, names, 'DESC');
    
    //create sum of value 7 and up
    for(var c = 8 ; c < a ; c++)
    {
      values[7] += values[c];
    }
    a = (a > 8) ? 8 : a;
    names[7] = "others";
    
    values = values.splice(0, 8);

    var legendaSpace = this.drawLegenda(names, a, plot, values, a);
    
    var startArc = -0.5 * Math.PI;
    var radius = Math.min((this.height - this.topOffset - this.bottomOffset), (this.width/this.numPlots) - legendaSpace - 10)/2;
    var midpoint_x = this.width/(2*this.numPlots) + plot * this.width/(this.numPlots) - legendaSpace/2;
    
    for(var b = 0 ; b < a ; b++)
    {
      var arcSize = values[b]/total * 2 * Math.PI;
      
      this.canvas.beginPath();
      this.canvas.arc(midpoint_x, (this.height - this.topOffset - this.bottomOffset)/2 + this.topOffset, radius, startArc, startArc + arcSize, false);
      this.canvas.strokeStyle = "black";
      this.canvas.stroke();
      
      this.canvas.beginPath();
      this.canvas.moveTo(midpoint_x, (this.height - this.topOffset - this.bottomOffset)/2 + this.topOffset);
      this.canvas.arc(midpoint_x, (this.height - this.topOffset - this.bottomOffset)/2 + this.topOffset, radius, startArc, startArc + arcSize, false);
      startArc += arcSize;
      this.canvas.fillStyle = this.generateColor(b, this.PieColorType);
      this.canvas.fill();
    }
    
    plot++;
  }
  return;
};

KiwiPieChart.prototype.setPieColorwheel = function(PieColorType, PieColorwheel)
{
  this.PieColorType = PieColorType;
  this.PieColorwheel = PieColorwheel;
  
  this.redraw();
};